//
// Created by 吴喆 on 2020/3/17.
//

#include "AtkStateMachine.h"
#include "../DemoPlayer.h"

PlayableState *AtkState::handleInput(Playable &playable, KeyEvent *event) {
//    if (event->key == GLFW_KEY_X) {
//        if (event->released) {
//            return new NotAtkState();
//        }
//    }
    return nullptr;
}

PlayableState *AtkState::update(Playable &playable) {
    auto player = dynamic_cast<DemoPlayer *>(&playable);
    lastTime += CTime::getInstance()->getDelta();
    if (player) {
        if (Input::getInstance()->keyMap[GLFW_KEY_X] && (Input::getInstance()->keyMap[GLFW_KEY_X]->released) &&
            (lastTime >= player->atkInterval)) {
            return new NotAtkState();
        }

        if (lastTime >= player->atkInterval) {
            player->launchBullet();
            lastTime = 0;
        }

    }
    return nullptr;
}

void AtkState::enter(Playable &playable) {
    playable.isAtk = true;
    auto player = dynamic_cast<DemoPlayer *>(&playable);
    if (player) {
//        player->launchBullet();
        lastTime = player->atkInterval;
    }
}


AtkStateMachine::AtkStateMachine() {
    currentState = new NotAtkState();
}

PlayableState *NotAtkState::handleInput(Playable &playable, KeyEvent *event) {
    if (event->key == GLFW_KEY_X) {
        if (event->held || event->down) {
            return new AtkState();
        }
    }
    return nullptr;
}

PlayableState *NotAtkState::update(Playable &playable) {
    if (Input::getInstance()->keyMap[GLFW_KEY_X] && (Input::getInstance()->keyMap[GLFW_KEY_X]->down
                                                     || Input::getInstance()->keyMap[GLFW_KEY_X]->held)) {
        return new AtkState();
    }
    return nullptr;
}

void NotAtkState::enter(Playable &playable) {
    playable.isAtk = false;

}
